<template>
  <div class="demand-hall-container">
    <!-- 页面标题 -->
    <div class="page-header">
      <h1>需求大厅</h1>
      <p>浏览最新的需求信息，寻找适合您企业的合作机会</p>
    </div>

    <!-- 搜索和筛选区域 -->
    <div class="search-filter-card">
      <el-form :inline="true" :model="searchParams" class="search-form">
        <el-form-item label="关键词">
          <el-input
            v-model="searchParams.keyword"
            placeholder="输入需求标题关键词"
            clearable
            style="width: 240px;"
          />
        </el-form-item>
        
        <el-form-item label="预算范围">
          <el-input-number
            v-model="searchParams.minBudget"
            placeholder="最低"
            :min="0"
            :step="100"
            style="width: 120px;"
          />
          <span class="budget-separator">至</span>
          <el-input-number
            v-model="searchParams.maxBudget"
            placeholder="最高"
            :min="0"
            :step="100"
            style="width: 120px;"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 需求列表区域 -->
    <div v-if="loading" class="loading-container">
      <el-skeleton :rows="5" animated />
    </div>
    
    <div v-else-if="demands.length === 0" class="empty-state">
      <el-empty description="暂无需求信息" />
    </div>
    
    <div v-else class="demands-list">
      <div class="demand-card" v-for="demand in demands" :key="demand.id" @click="handleCardClick(demand)">
        <div class="demand-header">
          <h3 class="demand-title">{{ demand.title }}</h3>
          <el-tag :type="getStatusTagType(demand.status)">{{ getStatusText(demand.status) }}</el-tag>
        </div>
        
        <div class="demand-info">
          <div class="info-item">
            <span class="info-label">预算：</span>
            <span class="info-value budget-value">{{ formatBudget(demand.budget) }}</span>
          </div>
          <div class="info-item">
            <span class="info-label">截止日期：</span>
            <span class="info-value">{{ demand.deadline }}</span>
          </div>
          <div class="info-item">
            <span class="info-label">发布时间：</span>
            <span class="info-value">{{ demand.publishTime }}</span>
          </div>
        </div>
        
        <div class="demand-description">
          <p>{{ truncateText(demand.description, 100) }}</p>
        </div>
        
        <div v-if="demand.acceptedEnterprise" class="accepted-enterprise">
          <span class="enterprise-label">已接单企业：</span>
          <span class="enterprise-name">{{ demand.acceptedEnterprise.name }}</span>
        </div>
      </div>
    </div>

    <!-- 分页组件 -->
    <div v-if="demands.length > 0" class="pagination-container">
      <el-pagination
        v-model:current-page="pagination.currentPage"
        v-model:page-size="pagination.pageSize"
        :page-sizes="[10, 20, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 需求详情对话框 -->
    <el-dialog
      v-model="detailDialogVisible"
      title="需求详情"
      width="70%"
      destroy-on-close
    >
      <div v-if="currentDemand" class="demand-detail-content">
        <div class="detail-header">
          <h2 class="detail-title">{{ currentDemand.title }}</h2>
          <el-tag :type="getStatusTagType(currentDemand.status)">{{ getStatusText(currentDemand.status) }}</el-tag>
        </div>
        
        <div class="detail-info">
          <div class="detail-info-row">
            <div class="detail-item">
              <span class="detail-label">预算：</span>
              <span class="detail-value budget-value">{{ formatBudget(currentDemand.budget) }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">截止日期：</span>
              <span class="detail-value">{{ currentDemand.deadline }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">发布时间：</span>
              <span class="detail-value">{{ currentDemand.publishTime }}</span>
            </div>
          </div>
          
          <div v-if="currentDemand.acceptedEnterprise" class="accepted-enterprise-detail">
            <span class="enterprise-detail-label">已接单企业：</span>
            <div class="enterprise-info">
              <div class="enterprise-name-detail">{{ currentDemand.acceptedEnterprise.name }}</div>
              <div class="enterprise-desc">{{ currentDemand.acceptedEnterprise.description }}</div>
            </div>
          </div>
        </div>
        
        <div class="detail-section">
          <h3 class="section-title">需求描述</h3>
          <div class="description-content">{{ currentDemand.description }}</div>
        </div>
        
        <div class="detail-section">
          <h3 class="section-title">需求时间线</h3>
          <div class="timeline">
            <div class="timeline-item">
              <span class="timeline-label">创建时间：</span>
              <span class="timeline-value">{{ currentDemand.createTime }}</span>
            </div>
            <div class="timeline-item">
              <span class="timeline-label">发布时间：</span>
              <span class="timeline-value">{{ currentDemand.publishTime }}</span>
            </div>
            <div class="timeline-item">
              <span class="timeline-label">最后更新：</span>
              <span class="timeline-value">{{ currentDemand.updateTime }}</span>
            </div>
          </div>
        </div>
      </div>
      
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="closeDetailDialog">关闭</el-button>
          <el-button 
            v-if="currentDemand && currentDemand.status === 3"
            type="primary" 
            @click="acceptDemand(currentDemand.id)"
          >
            接受需求
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { onMounted } from 'vue';
import { useDemandHall } from '../hooks/useDemandHall';

// 使用需求大厅的hook
const {
  // 状态
  loading,
  demands,
  currentDemand,
  detailDialogVisible,
  pagination,
  searchParams,
  
  // 方法
  fetchDemandList,
  viewDemandDetail,
  closeDetailDialog,
  acceptDemand,
  handleSearch,
  handleReset,
  handleSizeChange,
  handleCurrentChange,
  getStatusText,
  getStatusTagType,
  formatBudget,
  truncateText
} = useDemandHall();

// 处理卡片点击事件
const handleCardClick = async (demand) => {
  await viewDemandDetail(demand);
};

// 组件挂载时获取需求列表
onMounted(() => {
  fetchDemandList();
});
</script>

<style scoped>
.demand-hall-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.page-header {
  text-align: center;
  margin-bottom: 30px;
}

.page-header h1 {
  font-size: 32px;
  color: #303133;
  margin-bottom: 10px;
  font-weight: 600;
}

.page-header p {
  font-size: 16px;
  color: #606266;
  margin: 0;
}

.search-filter-card {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}

.search-form {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

.budget-separator {
  margin: 0 8px;
  color: #606266;
}

.loading-container {
  min-height: 400px;
  padding: 20px;
}

.empty-state {
  min-height: 400px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.demands-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(380px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}

.demand-card {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  cursor: pointer;
}

.demand-card:hover {
  box-shadow: 0 4px 20px 0 rgba(0, 0, 0, 0.15);
  transform: translateY(-2px);
}

.demand-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 15px;
}

.demand-title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin: 0;
  flex: 1;
  margin-right: 10px;
  line-height: 1.4;
}

.demand-info {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  margin-bottom: 15px;
  padding-bottom: 15px;
  border-bottom: 1px solid #ebeef5;
}

.info-item {
  display: flex;
  align-items: center;
}

.info-label {
  font-size: 14px;
  color: #606266;
  margin-right: 5px;
}

.info-value {
  font-size: 14px;
  color: #303133;
  font-weight: 500;
}

.budget-value {
  color: #f56c6c;
  font-size: 16px;
}

.demand-description {
  margin-bottom: 15px;
}

.demand-description p {
  font-size: 14px;
  color: #606266;
  line-height: 1.6;
  margin: 0;
}

.accepted-enterprise {
  padding-top: 10px;
  border-top: 1px solid #ebeef5;
  display: flex;
  align-items: center;
}

.enterprise-label {
  font-size: 14px;
  color: #606266;
  margin-right: 5px;
}

.enterprise-name {
  font-size: 14px;
  color: #409eff;
  font-weight: 500;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

/* 详情对话框样式 */
.demand-detail-content {
  padding: 10px 0;
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 20px;
  padding-bottom: 20px;
  border-bottom: 2px solid #ebeef5;
}

.detail-title {
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  margin: 0;
  flex: 1;
  margin-right: 20px;
  line-height: 1.4;
}

.detail-info {
  margin-bottom: 30px;
}

.detail-info-row {
  display: flex;
  flex-wrap: wrap;
  gap: 30px;
  margin-bottom: 20px;
}

.detail-item {
  display: flex;
  align-items: center;
}

.detail-label {
  font-size: 16px;
  color: #606266;
  margin-right: 10px;
  font-weight: 500;
}

.detail-value {
  font-size: 16px;
  color: #303133;
}

.accepted-enterprise-detail {
  padding: 15px;
  background: #ecf5ff;
  border-radius: 6px;
  border-left: 4px solid #409eff;
}

.enterprise-detail-label {
  font-size: 16px;
  color: #409eff;
  font-weight: 500;
  display: block;
  margin-bottom: 10px;
}

.enterprise-info {
  margin-left: 20px;
}

.enterprise-name-detail {
  font-size: 18px;
  color: #303133;
  font-weight: 600;
  margin-bottom: 5px;
}

.enterprise-desc {
  font-size: 14px;
  color: #606266;
  line-height: 1.6;
}

.detail-section {
  margin-bottom: 30px;
}

.section-title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid #ebeef5;
}

.description-content {
  font-size: 16px;
  color: #303133;
  line-height: 1.8;
  white-space: pre-wrap;
}

.timeline {
  padding-left: 20px;
}

.timeline-item {
  position: relative;
  padding-left: 30px;
  margin-bottom: 15px;
}

.timeline-item::before {
  content: '';
  position: absolute;
  left: 0;
  top: 6px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #409eff;
}

.timeline-item::after {
  content: '';
  position: absolute;
  left: 3.5px;
  top: 14px;
  width: 1px;
  height: calc(100% + 15px);
  background: #ebeef5;
}

.timeline-item:last-child::after {
  display: none;
}

.timeline-label {
  font-size: 14px;
  color: #606266;
  margin-right: 10px;
}

.timeline-value {
  font-size: 14px;
  color: #303133;
  font-weight: 500;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .demands-list {
    grid-template-columns: 1fr;
  }
  
  .search-form {
    flex-direction: column;
    align-items: stretch;
  }
  
  .detail-info-row {
    flex-direction: column;
    gap: 15px;
  }
  
  .detail-header {
    flex-direction: column;
    align-items: stretch;
  }
  
  .detail-title {
    margin-bottom: 15px;
  }
}
</style>